package SignAndRegister;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.server.RMIClassLoaderSpi;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class RegisterWindowHandler implements ActionListener , DocumentListener{

    private RegisterWindow registerWindow;

    public RegisterWindowHandler(RegisterWindow registerWindow){this.registerWindow=registerWindow;}

    boolean registerSuccess = false;

    @Override
    public void actionPerformed(ActionEvent e) {

        String actionCommand = e.getActionCommand();
        System.out.println("actionCommand = "+actionCommand);

        if("confirmRegister".equals(actionCommand)){
            processConfirmRegister();
        } else if ("changeAccountType".equals(actionCommand)) {
            try {
                this.processIDTextChanges();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }

        if(registerSuccess) registerWindow.dispose();

    }

    private void processConfirmRegister(){

        String accountID = registerWindow.getIDText();
        String accountCode = registerWindow.getCodeText();
        String accountType = registerWindow.getTypeChoice();

        //先检查ID是否已经存在，若存在则报错修改

        try {

            String nowIDText = registerWindow.getIDText().toString();
            String nowAccountType = registerWindow.getTypeChoice();

            Class.forName("com.mysql.cj.jdbc.Driver"); //注册驱动
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hims", "root", "511300");  //获取连接
            Statement stat = con.createStatement(); //获取执行者对象

            String targetAccountID = null;

            if("住客".equals(nowAccountType)){

                if(nowIDText.length()==11){

                    String sqlPhone = "SELECT customerPhone FROM customer WHERE customerPhone=" + nowIDText;
                    ResultSet rs = stat.executeQuery(sqlPhone);


                    while (rs.next()){
                        targetAccountID = rs.getString("customerPhone");
                    }

                    if(targetAccountID==null){
                        //在此进行注册
                        //规定五位数以内的编号为职工、酒店管理员和系统管理员的编号，五位数至十位数之间的编号为顾客的编号，就算如此，顾客也有90亿的存量
                        //先对customer表进行插入customerPhone，customer表中的customerID自动生成，再对customer表进行查询，获取当前手机号下的顾客ID
                        String sqlInsertPhone = "INSERT INTO customer (customerPhone) VALUES ('"+nowIDText+"')";
                        stat.execute(sqlInsertPhone);
                        String sqlIDPhone = "SELECT customerID FROM customer where customerPhone=" + nowIDText;
                        rs = stat.executeQuery(sqlIDPhone);
                        while (rs.next()){
                            accountID = rs.getString("customerID");
                        }
                        //获取之后，对account表进行插入
                        String sqlInsert = "INSERT INTO account VALUES ('"+accountID+"','"+accountCode+"','"+accountType+"')";
                        stat.execute(sqlInsert);
                        //弹出注册成功的提醒界面
                        new RemindWindow("CustomerRegisterSuccess");
                        registerSuccess = true;
                    }else {
                        new RemindWindow("Register");   //不可以用该ID注册
                    }
                }else{
                    new RemindWindow("Register");   //不可以用该ID注册
                }
            }else{
                if(nowIDText.length()!=0) {

                    String sql = "SELECT accountID FROM account WHERE accountID=" + nowIDText;
                    ResultSet rs = stat.executeQuery(sql);

                    while (rs.next()){
                        Integer accountIDTmp = rs.getInt("accountID");
                        targetAccountID = accountIDTmp.toString();
                    }

                    if(targetAccountID==null && nowIDText.length()<=5){
                        //对于酒店内部员工的账号申请，数据首先只存放在accountTmp表中，具体是否放入account表中、employee表、hotelAdmin表和systemAdmin表由系统管理员登录后决定
                        String sqlInsert = "INSERT INTO accountTmp VALUES ('"+accountID+"','"+accountCode+"','"+accountType+"')";
                        stat.execute(sqlInsert);
                        registerSuccess = true;
                        new RemindWindow("OtherRegisterSuccess");
                    }else{
                        new RemindWindow("Register");
                    }
                }
            }

            stat.close();
            con.close();

        }catch (Exception e){
            e.printStackTrace();
        }



    }

    private void processIDTextChanges() throws Exception{

        String nowIDText = registerWindow.getIDText().toString();
        String nowAccountType = registerWindow.getTypeChoice();

        Class.forName("com.mysql.cj.jdbc.Driver"); //注册驱动
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hims", "root", "511300");  //获取连接
        Statement stat = con.createStatement(); //获取执行者对象

        String targetAccountID = null;

        if("住客".equals(nowAccountType)){

            if(nowIDText.length()==11){

                String sqlPhone = "SELECT customerPhone FROM customer WHERE customerPhone=" + nowIDText;
                ResultSet rs = stat.executeQuery(sqlPhone);


                while (rs.next()){
                    targetAccountID = rs.getString("customerPhone");
                }

                if(targetAccountID==null){
                    registerWindow.changeTrueIcon();    //可以用该ID注册，弹出绿色提醒
                }else {
                    registerWindow.changeFalseIcon();   //不可以用该ID注册，弹出红色警示
                }

            }else{
                registerWindow.changeFalseIcon();   //不可以用该ID注册，弹出红色警示
            }
        }else{
            if(nowIDText.length()!=0) {

                String sql = "SELECT accountID FROM account WHERE accountID=" + nowIDText;
                ResultSet rs = stat.executeQuery(sql);

                while (rs.next()){
                    Integer accountIDTmp = rs.getInt("accountID");
                    targetAccountID = accountIDTmp.toString();
                }

                if(targetAccountID==null && nowIDText.length()<=5){
                    registerWindow.changeTrueIcon();    //可以用该ID注册，弹出绿色提醒
                }else{
                    registerWindow.changeFalseIcon();   //不可以用该ID注册，弹出红色警示
                }
            }
        }

        stat.close();
        con.close();

    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        try {
            this.processIDTextChanges();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        try {
            this.processIDTextChanges();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        try {
            this.processIDTextChanges();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}
